<?php
/*
 * This file is part of the BooXtore Student project.
 *
 * (c) Francis Backeland f.backeland@gmail.com
 * (c) Laurine Chaplot chaplot.laurine@orange.fr
 * (c) Julien Dutot julien.dutot@gmail.com
 * (c) Romain Guillaume romain-guillaume@orange.fr
 * (c) Guillaume Paringaux g.paringaux@gmail.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code
 * 
 */

namespace BXTR\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\Common\Util\Inflector;

/**
 * Class AdminListForm
 */
class AdminListForm extends AbstractAdminForm
{
    protected $em;

    public function __construct($em)
    {
        $this->em = $em;
    }

    public function buildControls(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('back', 'back_button', array(
                'url' => $options['back_url']
            ))
            ->add('add', 'add_button')
            ->add('delete', 'datatables_popup_button', array(
                'ajax_url' => $options['remove_popup_url'],
            ))
            ->add('edit', 'edit_button', array(
                'datatables_selector' => true,
            ));
    }

    public function buildBody(FormBuilderInterface $builder, array $options)
    {
        $qb = $options['query_builder'];
        if (!$qb) {
            // default queryBuilder
            $qb = $this->em->createQueryBuilder();
            $qb
                ->add('select', 'e') 
                ->add('from', $options['class'] . ' e'); 
        }
        $fieldTransformers = array();
        $fieldNames = array();
        $fieldLabels = array();
        foreach($options['fields'] as $fieldName => $fieldParams) {
            if (in_array($fieldParams['visibility'], array('list', 'both'))) { 
                $transformer = $fieldParams['transformer'];
                if ($transformer !== null) {
                    $fieldTransformers[$fieldName] = $transformer;
                }

                $label = $fieldParams['label'];
                if ($label != null) {
                    $fieldLabels[$fieldName] = $label;
                }

                $fieldNames[] = $fieldName;
            }
        }
        $builder
            ->add('list', 'datatables_selector', Array(
                  'query_builder'   => $qb,
                  'fieldNames'          => $fieldNames,
                  'class'           => $options['class'],
                  'fieldLabels'          => $fieldLabels,
                  'fieldTransformers'    => $fieldTransformers,
            ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'query_builder' => null,
        ));

        $resolver->setRequired(array(
            'fields', // String[] list of field to be displayed in the table
            'class', // String entity repository name
            'remove_popup_url', // String route of the ajax popup action
            'back_url',
        ));

        $resolver->setNormalizers(array(
            'fields' => function($options, $value) {
                if ($value === null || $value == array()) {
                    $value = array();
                    $fields = $this->getEntityMetadata($options['class'])->fieldNames;

                    foreach($fields as $fieldName) {
                        $value[] = Inflector::camelize($fieldName);
                    }

                }
               return $value; 
            }
        ));
    }

    // duplicated
    protected function getEntityMetadata($class)
    {
        return $this->em->getClassMetadata($class);
    }

    public function getName()
    {
        return 'bxtr_admin_list';
    }
}
